package com.xms.ribbonbutton.service;

import com.xms.core.exception.XmsException;
import com.xms.datacore.service.CreaterService;
import com.xms.ribbonbutton.entity.RibbonButton;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.Collection;

/**
 * <p>
 * RibbonButtonCreaterService
 * </p>
 *
 * @author migo
 * @since 2021/9/8
 */
@CacheConfig(cacheNames = "ribbonbutton", keyGenerator = "CacheKeyGenerator")
@Service
public class RibbonButtonCreaterService extends CreaterService<RibbonButton> implements IRibbonButtonCreaterService {
    private IRibbonButtonDependency _ribbonButtonDependency;
    @Autowired
    public RibbonButtonCreaterService(IRibbonButtonDependency ribbonButtonDependency){
        _ribbonButtonDependency = ribbonButtonDependency;
    }

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean create(RibbonButton entity) throws SQLException, XmsException {
        boolean success = super.save(entity);
        if(success){
            _ribbonButtonDependency.create(entity);
        }
        return success;
    }

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean createMany(Collection<RibbonButton> entityList) throws SQLException, XmsException {
        for (RibbonButton entity : entityList){
            this.create(entity);
        }
        return true;
    }
}
